package com.zaoke.web.component.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zaoke.web.bo.query.zone.Bus;
import com.zaoke.web.bo.query.zone.Place;
import com.zaoke.web.bo.query.zone.PlaceMod;
import com.zaoke.web.bo.query.zone.ZoneMod;
import com.zaoke.web.bo.query.zone.ZoneQuery;
import com.zaoke.web.bo.query.zone.ZoneSave;
import com.zaoke.web.bo.zone.Zone;
import com.zaoke.web.service.zone.ZoneService;
import com.zaoke.web.utils.JacksonUtil;
import com.zaoke.web.utils.WebResponse;

@Controller
@RequestMapping("/zone/")
public class ZoneController {
	
	@Autowired
	private ZoneService zoneService;

	@RequestMapping(value="zone-place-new.do",method = RequestMethod.GET)  
    public String placenew(HttpServletRequest request, HttpServletResponse response) {
		return "zone-new-place";
    }
	@RequestMapping(value="zone-place-mod.do",method = RequestMethod.GET)  
    public ModelAndView placemod(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mav = new ModelAndView();
		int zoneid = Integer.valueOf(request.getParameter("zoneid"));
		mav.setViewName("zone-mod-place");
		Zone zone = zoneService.getZoneById(zoneid,true);
		PlaceMod mod = new PlaceMod();
		
		if(zone!=null && zone.getParentId()!=null)
		{
			mod.setId(zone.getId());
			mod.setParentId(zone.getParentId());
			mod.setName(zone.getName());
			mod.setPickStartTime(zone.getPickStartTime());
			mod.setPickEndTime(zone.getPickEndTime());
			mod.setSequence(zone.getSequence());
			Zone bus = zoneService.getZoneById(zone.getParentId(),true);
			if(bus!=null && bus.getParentId()!=null)
			{
				Zone region = zoneService.getZoneById(bus.getParentId(),true);
				mod.setRegionId(region.getId());
			}
		}
		
		mav.addObject("zone",mod);
		return mav;
    }
	
	@RequestMapping(value="zone-bus-new.do",method = RequestMethod.GET)  
    public String busnew(HttpServletRequest request, HttpServletResponse response) {
		return "zone-new-bus";
    }
	
	@RequestMapping(value="zone-bus-mod.do",method = RequestMethod.GET)  
    public ModelAndView busmod(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		int zoneid = Integer.valueOf(request.getParameter("zoneid"));
		mav.setViewName("zone-mod-bus");
		mav.addObject("zone",zoneService.getZoneById(zoneid,true));
		return mav;
    }
	
	@RequestMapping(value="zone-bus-del.do",method = RequestMethod.GET)  
    public void busdel(HttpServletRequest request, HttpServletResponse response) {
		Long zoneid = Long.valueOf(request.getParameter("zoneid"));
		zoneService.deleteZone(zoneid);
//		mav.setViewName("zone-mod-bus");
//		mav.addObject("zone",zoneService.getZoneById(zoneid,true));
//		return mav;
    }
	
	@RequestMapping(value="zone-place-del.do",method = RequestMethod.GET)  
    public void placedel(HttpServletRequest request, HttpServletResponse response) {
		Long zoneid = Long.valueOf(request.getParameter("zoneid"));
		zoneService.deleteZone(zoneid);
//		mav.setViewName("zone-mod-bus");
//		mav.addObject("zone",zoneService.getZoneById(zoneid,true));
//		return mav;
    }
	
	@ResponseBody
	@RequestMapping(value="userQueryZone.ajax",method = RequestMethod.POST) 
	public WebResponse<ArrayList<ZoneQuery>> userQueryZone(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<ArrayList<ZoneQuery>> wr = new WebResponse<ArrayList<ZoneQuery>>();
		Set<Zone> zones = zoneService.getAllZonesInType(Zone.TYPE_PICK,true);
		ArrayList<ZoneQuery> res = new ArrayList<ZoneQuery>();
		if(zones!=null && zones.size()>0)
		{
			for(Zone zone : zones)
			{
				ZoneQuery query = new ZoneQuery();
				query.setZoneId(zone.getId());
				query.setZoneName(zone.getName());
				res.add(query);
			}
		}
		wr.setData(res);
		return wr;
	}
	
	@ResponseBody
	@RequestMapping(value="saveZonePlace.ajax",method = RequestMethod.POST) 
	public WebResponse<Boolean> saveZonePlace(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<Boolean> wr = new WebResponse<Boolean>();
		ZoneSave input;
		try {
			input = JacksonUtil.readObj(request.getInputStream(), ZoneSave.class);
			input.setType(Zone.TYPE_PICK);
			wr.setData(zoneService.newZone(input));
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wr;
	}
	
	@ResponseBody
	@RequestMapping(value="saveZonebus.ajax",method = RequestMethod.POST) 
	public WebResponse<Boolean> saveZonebus(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<Boolean> wr = new WebResponse<Boolean>();
		ZoneSave input;
		try {
			input = JacksonUtil.readObj(request.getInputStream(), ZoneSave.class);
			input.setType(Zone.TYPE_MERCHANT);
			wr.setData(zoneService.newZone(input));
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wr;
	}
	
	@ResponseBody
	@RequestMapping(value="updateZonebus.ajax",method = RequestMethod.POST) 
	public WebResponse<Boolean> updateZonebus(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<Boolean> wr = new WebResponse<Boolean>();
		ZoneMod input;
		try {
			input = JacksonUtil.readObj(request.getInputStream(), ZoneMod.class);
			input.setType(Zone.TYPE_MERCHANT);
			wr.setData(zoneService.updateZone(input));
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wr;
	}
	
	@ResponseBody
	@RequestMapping(value="updateZoneplace.ajax",method = RequestMethod.POST) 
	public WebResponse<Boolean> updateZoneplace(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<Boolean> wr = new WebResponse<Boolean>();
		ZoneMod input;
		try {
			input = JacksonUtil.readObj(request.getInputStream(), ZoneMod.class);
			input.setType(Zone.TYPE_PICK);
			wr.setData(zoneService.updateZone(input));
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wr;
	}
	
	@ResponseBody
	@RequestMapping(value="listRegion.ajax",method = RequestMethod.POST) 
	public WebResponse<ArrayList<Zone>> listRegion(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<ArrayList<Zone>> wr = new WebResponse<ArrayList<Zone>>();
		int type = Zone.TYPE_DISTRICT;
		Set<Zone> set = zoneService.getAllZonesInType(type,true);
		ArrayList<Zone> res = new ArrayList<Zone>(set);
		wr.setData(res);
		return wr;
	}
	@ResponseBody
	@RequestMapping(value="listBus.ajax",method = RequestMethod.POST) 
	public WebResponse<ArrayList<Bus>> listBus(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<ArrayList<Bus>> wr = new WebResponse<ArrayList<Bus>>();
		int type = Zone.TYPE_MERCHANT;
		Set<Zone> set = zoneService.getAllZonesInType(type,true);
		ArrayList<Bus> res = new ArrayList<Bus>();
		if(set!=null && set.size()>0)
		{
			for(Zone zone : set)
			{
				Bus bus = new Bus();
				bus.setId(zone.getId());
				bus.setBusName(zone.getName());
				if(zone.getParentId()!=null)
				{
					Zone pzone = zoneService.getZoneById(zone.getParentId(),true);
					if(pzone!=null)
					{
						bus.setRegionName(pzone.getName());
					}
				}
				bus.setState(transZoneState(zone.getState()));
				res.add(bus);
			}
		}
		
		wr.setData(res);
		return wr;
	}
	
	@ResponseBody
	@RequestMapping(value="listPlace.ajax",method = RequestMethod.POST) 
	public WebResponse<ArrayList<Place>> listPlace(HttpServletRequest request, HttpServletResponse response)
	{
		WebResponse<ArrayList<Place>> wr = new WebResponse<ArrayList<Place>>();
		int type = Zone.TYPE_PICK;
		Set<Zone> set = zoneService.getAllZonesInType(type,true);
		ArrayList<Place> res = new ArrayList<Place>();
		if(set!=null && set.size()>0)
		{
			for(Zone zone : set)
			{
				Place place = new Place();
				place.setId(zone.getId());
				place.setPickName(zone.getName());
				if(zone.getParentId()!=null && zone.getParentId() > 0)
				{
					Zone bus = zoneService.getZoneById(zone.getParentId(),true);
					if(bus!=null)
					{
						place.setBusName(bus.getName());
						if(bus.getParentId()!=null && bus.getParentId() > 0)
						{
							Zone region = zoneService.getZoneById(bus.getParentId(),true);
							if(region!=null)
							{
								place.setRegionName(region.getName());
							}
						}
					}
				}
				place.setPickstart(zone.getPickStartTime());
				place.setPickend(zone.getPickEndTime());
				place.setState(transZoneState(zone.getState()));
				place.setSequence(zone.getSequence());
				res.add(place);
			}
		}
		
		wr.setData(res);
		return wr;
	}
	
	
	private String transZoneState(int state)
	{
		switch(state)
		{
		case Zone.STATE_ACTIVE:
			return "正常使用";
		case Zone.STATE_INACTIVE:
			return "停止使用";
		}
		return "";
	}
	
}
